var page = require('webpage').create();
var system = require('system'), keyWords;
var fs = require('fs');
if(system.args.length < 2) {
	console.log("phantomjs usage is: phantomjs XX.js keywords");
	phantom.exit();
}else {
	keyWords = system.args[1];
}
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36';
page.settings.viewportSize = { width: 1280, height: 800 };
page.open('https://www.baidu.com', function(status) {
	if(status == 'success') {
		var linkArray = [], writeInfo = '', unable_open = '', pageIndex = 0, countUrlLength = 0, exitFlag = 0, dataJson = {};
		page.includeJs('jquery.js', function() {
			if(page.injectJs('clickFunction.js')) {
				page.evaluate(function(word) {
					document.getElementById("kw").value = word;
				    var resultBtn = document.getElementById("su");
				    ele_click(resultBtn);
				}, keyWords);
			}
		});
		
		setTimeout(function() {
			checkStatus();
		}, 100)
		timer = setInterval(function() {
			checkStatus();
			if(parseInt(pageIndex) >= 3) {
				clearInterval(timer);
				outTimer = setInterval(function() {
					if(exitFlag == countUrlLength) {
						clearInterval(outTimer);
						var last = JSON.stringify(dataJson);
						var oData = new Date();
						var year = oData.getFullYear();
						var month = oData.getMonth() + 1;
						var day = oData.getDate();
						var hour = oData.getHours(); //获取系统时，
						var minutes = oData.getMinutes(); //分
						fs.write('baiduJson/'+ keyWords + '_'+ year + '_' + month + '_' + day +'_' + hour + '_' + minutes+ '.txt', last, 'w');
						phantom.exit();
					}
				}, 100)
			}
			
		}, 7000);
		var  outJson = {};
		page.onPageCreated = function(newPage) {
			var startTime, currentPage = 0;
			if(currentPage != pageIndex) {
				outJson = {};
			}
			currentPage = pageIndex;
			newPage.onLoadStarted = function() {
				startTime = new Date().getTime();
				var t = window.setInterval(function() {
					var present = new Date().getTime();
					if(present > startTime + 5*1000) {
						clearInterval(t);
						var href =  newPage.evaluate(function() {
							return window.location.href;
						})
						var tit = newPage.evaluate(function() {
							return document.title;
						})
						if(href != "about:blank") {
							outJson[href] = tit;
						}
						dataJson[currentPage] = outJson;
						exitFlag++;
						newPage.close();
					}else {
						if(newPage.url.length > 0){							
							exitFlag++;
							clearInterval(t);
							outJson[newPage.url] = newPage.title;
							dataJson[currentPage] = outJson;
							newPage.close();
						}
					}
				}, 200)
			}
			newPage.settings.resourceTimeout = 5000;
			newPage.onResourceTimeout = function(e) {
				console.log("e.url"+ e.url);
				console.log("e.code"+ e.Code);
				console.log("e.string" + e.String);
			};
			
		};
		
		function checkStatus() {
			pageIndex++;
			var urlLength = 0;
			var readyState = page.evaluate(function () {
				return document.readyState;
			});
			if(readyState == "complete") {
				page.includeJs('jquery.js', function() {
					if(page.injectJs('clickFunction.js')) {
						page.evaluate(function(pIndex) {
							var linkLength = document.querySelectorAll("#content_left .result.c-container h3").length;
							for(var i = 0; i < linkLength; i++) {
								var wrapH3 = document.querySelectorAll("#content_left .result.c-container h3")[i];
								var a = wrapH3.getElementsByTagName('a')[0];
								ele_click(a);
							}
							var next = document.querySelectorAll("#page .pc")[pIndex];
							ele_click(next);
						}, pageIndex)
					}
				});
				setTimeout(function() {
					urlLength = page.evaluate(function() {
						return document.querySelectorAll('#content_left .result.c-container h3').length;	
					});
					countUrlLength = countUrlLength + urlLength;
					console.log("all url length: " + countUrlLength);
				}, 50)
			}else {
				checkStatus();
			}
		}
	}
})



